package nsqp

import (
	"bgs/cache/redisImp"
	"bgs/component"
	"bgs/conf"
	"bgs/db"
	"bgs/grpc/client"
	"bgs/grpc/imp/enums"
	ledgerRunner "bgs/nsqp/runners/ledger"
	orderRunner "bgs/nsqp/runners/order"
	retryRunner "bgs/nsqp/runners/retry"
	"bgs/nsqp/util"
	activityService "bgs/service/activity"
	afterSaleService "bgs/service/afterSale"
	brandService "bgs/service/brand"
	enforcementService "bgs/service/enforcement"
	ledgerService "bgs/service/ledger"
	orderService "bgs/service/order"
	paymentService "bgs/service/payment"
	spokesmanService "bgs/service/spokesman"
	storeService "bgs/service/store"
	userService "bgs/service/user"
	wxpMinaService "bgs/wxp/mina"
	"go.uber.org/zap"
)

var (
	// GsmsForBgapiProxy proxy gsms for bgapi
	GsmsForBgapiProxy *GsmsProxy

	// InventoryForBgsProxy proxy inventory for bgs
	InventoryForBgsProxy *InventoryProxy

	// BgapiForBgsProxy proxy compensation for bgapi
	CompensationForBgsProxy *CompensationProxy

	// BgapiForBgsProxy proxy bgapi for bgs
	BgapiForBgsProxy *BgapiProxy

	// RunnerForBgsProxy proxy runner for bgs
	RunnerForBgsProxy *RunnerProxy

	// TriggerForBgApiProxy proxy trigger for bgapi
	TriggerForBgApiProxy *TriggerProxy

	// WxpayForBgsProxy proxy wxpay for bgs
	WxpayForBgsProxy *WxpayProxy

	// ProducerOfBgsProxy nsq producer proxy of bgs
	// ProducerOfBgsProxy *NsqProducerProxy

	// ForwardOfBgsProxy nsq message forwarding
	ForwardOfBgsProxy *ForwardProxy

	nsqLogger *util.NoopNSQLogger
)

const (
	appID = string(enums.APPIDBGS)
)

// InitNsqServer init all nsq proxies
func InitNsqServer(
	nsqConf conf.NsqSection,
	pgdao *db.PgDao, slog *zap.SugaredLogger,
	wechatConfPtr *conf.WechatSection,
	vars *conf.VarsSection,
	adp *cache.Adapter,
	clientProxy *client.GrpcClientProxy,
	cm *component.ComponentManger,
) (err error) {
	slog = slog.With("entry", "nsq")

	orderRunner, err := orderRunner.NewNsqOrderRunner(nsqConf, slog)
	if err != nil {
		slog.Fatalf("failed to init orderRunner: %v", err)
		return
	}
	ledgerRunner, err := ledgerRunner.NewNsqLedgerRunner(nsqConf, slog)
	if err != nil {
		slog.Fatalf("failed to init ledgerRunner: %v", err)
		return
	}
	retryRunner, err := retryRunner.NewNsqRetryRunner(nsqConf, slog)
	if err != nil {
		slog.Fatalf("failed to init retryRunner: %v", err)
		return
	}

	activityService := activityService.NewActivityService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp, cm)
	afterSaleService := afterSaleService.NewAfterSaleService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	brandService := brandService.NewBrandService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	enforcementService := enforcementService.NewEnforcementService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	ledgerService := ledgerService.NewLedgerService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp, ledgerRunner)
	orderService := orderService.NewOrderService(pgdao, slog, vars, clientProxy, adp, orderRunner)
	paymentService := paymentService.NewPaymentService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	spokesmanService := spokesmanService.NewSpokesmanServiceImp(pgdao, slog, vars, clientProxy, adp)
	storeService := storeService.New(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	userService := userService.NewUserService(pgdao, slog, wechatConfPtr, vars, clientProxy, adp)
	wxpMinaService := wxpMinaService.NewMinaService(pgdao, slog, wechatConfPtr, vars, adp)

	failover := util.NewNsqFailover(pgdao, slog, vars)
	nsqLogger = util.NewNoopNSQLogger(slog)

	ForwardOfBgsProxy, err = NewForwardProxy(nsqConf, pgdao, slog)
	if err != nil {
		slog.Fatalf("failed to init ForwardProxy: %v", err)
		return
	}

	GsmsForBgapiProxy, err = NewGsmsProxy(nsqConf, pgdao, slog)
	if err != nil {
		slog.Fatalf("failed to init GsmsForBgapiProxy: %v", err)
		return
	}

	InventoryForBgsProxy, err = NewInventoryProxy(nsqConf, pgdao, slog, vars, clientProxy, adp, failover,
		enforcementService, ledgerService, wxpMinaService)
	if err != nil {
		slog.Fatalf("failed to init InventoryForBgsProxy: %v", err)
		return
	}

	CompensationForBgsProxy, err = NewCompensationProxy(nsqConf, pgdao, slog, failover)
	if err != nil {
		slog.Fatalf("failed to init CompensationForBgsProxy: %v", err)
		return
	}

	BgapiForBgsProxy, err = NewBgapiProxy(nsqConf, pgdao, slog, failover, activityService, brandService, storeService, userService)
	if err != nil {
		slog.Fatalf("failed to init BgapiForBgsProxy: %v", err)
		return
	}

	RunnerForBgsProxy, err = NewRunnerProxy(nsqConf, pgdao, slog, failover, ledgerService)
	if err != nil {
		slog.Fatalf("failed to init RunnerForBgsProxy: %v", err)
		return
	}

	TriggerForBgApiProxy, err = NewTriggerProxy(nsqConf, pgdao, slog, failover, retryRunner, activityService, ledgerService, orderService, userService)
	if err != nil {
		slog.Fatalf("failed to init TriggerForBgApiProxy: %v", err)
		return
	}

	WxpayForBgsProxy, err = NewWxpayProxy(nsqConf, pgdao, slog, vars, clientProxy, adp, failover,
		activityService, afterSaleService, brandService, ledgerService, orderService, paymentService,
		spokesmanService, storeService, userService, wxpMinaService)
	if err != nil {
		slog.Fatalf("failed to init WxpayForBgsProxy: %v", err)
		return
	}

	// ProducerOfBgsProxy, err = NewNsqProducerProxy(nsqConf, slog)
	// if err != nil {
	// 	slog.Fatalf("failed to init ProducerOfBgsProxy: %v", err)
	// 	return
	// }

	slog.Info("nsq started")

	return
}
